package com.yxy.controller.mobile;

import com.yxy.common.Result;
import com.yxy.constant.RequestMapperConstant;
import com.yxy.domain.dto.UserDTO;
import com.yxy.domain.dto.UserLoginDTO;
import com.yxy.domain.vo.UserLoginVO;
import com.yxy.domain.vo.UserVO;
import com.yxy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userController")
@RequestMapping(RequestMapperConstant.USER_REQUEST_MAPPER + "user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserDTO userDTO){
        log.info("用户注册：{}", userDTO);
        userService.register(userDTO);
        return Result.success("注册成功");
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录：{}", userLoginDTO);
        UserLoginVO vo = userService.login(userLoginDTO);
        return Result.success("登录成功", vo);
    }

    /**
     * 校验密码
     * @param id
     * @param password
     * @return
     */
    @GetMapping("/verifyPwd")
    public Result verifyPwd(Integer id, String password){
        log.info("校验密码：{}, {}", id, password);
        userService.verifyPwd(id, password);
        return Result.success("");
    }

    /**
     * 修改密码
     * @return
     */
    @PutMapping("/updatePwd/{id}/{newPwd}")
    public Result<String> updatePwd(@PathVariable Integer id, @PathVariable String newPwd){
        log.info("修改密码：{}, {}", id, newPwd);
        userService.updatePwd(id, newPwd);
        return Result.success("修改密码成功");
    }

    /**
     * 校验手机号
     * @param id
     * @param phone
     * @return
     */
    @GetMapping("/verifyPhone")
    public Result verifyPhone(String username, String phone){
        log.info("校验手机号：{}, {}", username, phone);
        userService.verifyPhone(username, phone);
        return Result.success("");
    }

    /**
     * 重置密码
     * @return
     */
    @PutMapping("/resetPwd")
    public Result<String> resetPwd(@RequestBody UserDTO userDTO){
        log.info("重置密码：{}", userDTO);
        userService.resetPwd(userDTO);
        return Result.success("重置密码成功");
    }

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    public Result<UserVO> getUserInfo(Integer id){
        log.info("获取用户信息：{}", id);
        return Result.success("", userService.getUserInfo(id));
    }

    /**
     * 修改用户信息
     * @param userDTO
     * @return
     */
    @PutMapping("/update")
    public Result<String> updateUserInfo(@RequestBody UserDTO userDTO){
        log.info("修改用户信息：{}", userDTO);
        userService.updateUserInfo(userDTO);
        return Result.success("修改成功");
    }


    /**
     * 修改活动时间
     * @return
     */
    @PutMapping("/modify/{id}")
    public Result<String> modifyActivityTime(@PathVariable Integer id){
        log.info("修改活动时间：{}", id);
        String tip = userService.modifyActivityTime(id);
        return Result.success("", tip);
    }

}
